package com.smec.moblie.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * Created by sunyubo on 2017/3/16.
 */

public class AskActionActivity extends Activity {
    private Handler handler = new Handler();
    private WebView webView;
    private String closeUrl;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        webView = new WebView(this);
        setContentView(webView);
        initView();
    }

    protected void initView() {
//        webView = (WebView) findViewById(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);//支持javascript
        webView.requestFocus();//触摸焦点起作用mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);//取消滚动条
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);//设置允许js弹出alert对话框
        webView.getSettings().setSupportZoom(true);// 设置可以支持缩放
        webView.getSettings().setBuiltInZoomControls(true);// 设置出现缩放工具
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        String url = getIntent().getStringExtra("URL");
        closeUrl = getIntent().getStringExtra("CLOSE_URL");
        if (url == null || url.equals("")) {
            finish();
        } else {
            //设置WebViewClient
            webView.setWebViewClient(new MyWebViewClient());
            webView.loadUrl(url);
        }

    }


    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private class MyWebViewClient extends WebViewClient {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            if (closeUrl != null && url.equals(closeUrl)) {
                AskActionActivity.this.finish();
                MainActivity.isNeedAskAction = false;
            }
            super.onPageStarted(view, url, favicon);
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        ViewGroup view = (ViewGroup) getWindow().getDecorView();
        view.removeAllViews();
        webView.destroy();
    }
}
